package com.ruoyi.dance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dance.domain.bo.DancePageBo;
import com.ruoyi.dance.domain.vo.DancePageVo;
import com.ruoyi.dance.domain.DancePage;
import com.ruoyi.dance.mapper.DancePageMapper;
import com.ruoyi.dance.service.IDancePageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 页面管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-21
 */
@RequiredArgsConstructor
@Service
public class DancePageServiceImpl implements IDancePageService {

    private final DancePageMapper baseMapper;

    /**
     * 查询页面管理
     */
    @Override
    public DancePageVo queryById(Long pageId){
        return baseMapper.selectVoById(pageId);
    }


    /**
     * 查询页面管理列表
     */
    @Override
    public List<DancePageVo> queryList(DancePageBo bo) {
        LambdaQueryWrapper<DancePage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DancePage> buildQueryWrapper(DancePageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DancePage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), DancePage::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), DancePage::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getKeywords()), DancePage::getKeywords, bo.getKeywords());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), DancePage::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DancePage::getStatus, bo.getStatus());
        lqw.orderByDesc(DancePage::getOrderBy);
        return lqw;
    }

    /**
     * 新增页面管理
     */
    @Override
    public Boolean insertByBo(DancePageBo bo) {
        DancePage add = BeanUtil.toBean(bo, DancePage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPageId(add.getPageId());
        }
        return flag;
    }

    /**
     * 修改页面管理
     */
    @Override
    public Boolean updateByBo(DancePageBo bo) {
        DancePage update = BeanUtil.toBean(bo, DancePage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DancePage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除页面管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<DancePageVo> getMenuCache() {
        List<DancePageVo> dancePages = baseMapper.selectVoList(Wrappers.<DancePage>lambdaQuery()
            .eq(DancePage::getParentId, 0));

        DancePageVo dancePageVo = dancePages.get(0);

        dancePageVo
            .setChildren(baseMapper.selectVoList(Wrappers.<DancePage>lambdaQuery()
                .eq(DancePage::getParentId, dancePageVo.getPageId())
                .eq(DancePage::getStatus, 2)
                .orderByDesc(DancePage::getOrderBy)));

//        for (DancePageVo child : dancePageVo.getChildren()) {
//            if (child.getDescription() == null){
//                child.setDescription(dancePageVo.getDescription());
//            }
//            if (child.getKeywords() == null){
//                child.setKeywords(dancePageVo.getKeywords());
//            }
//        }

        return dancePageVo.getChildren();
    }

    @Override
    public DancePageVo getPageViewByPath(String path) {
        DancePageVo dancePageVo = baseMapper.selectVoOne(Wrappers.<DancePage>lambdaQuery()
            .eq(DancePage::getPath, path)
            .ne(DancePage::getParentId, 0));

        if (dancePageVo == null){
            return baseMapper.selectVoList(Wrappers.<DancePage>lambdaQuery()
                .eq(DancePage::getParentId, 0)).get(0);
        }

        DancePageVo parent = baseMapper.selectVoOne(Wrappers.<DancePage>lambdaQuery()
            .eq(DancePage::getPageId, dancePageVo.getParentId()));

        if (dancePageVo.getTitle().equals("首页")){
            dancePageVo.setTitle(parent.getTitle());
        }else {
            dancePageVo.setTitle(dancePageVo.getTitle() + " - " + parent.getTitle());
        }

        if (dancePageVo.getDescription() == null){
            dancePageVo.setDescription(parent.getDescription());
        }

        if (dancePageVo.getKeywords() == null){
            dancePageVo.setKeywords(parent.getKeywords());
        }

        return dancePageVo;
    }
}
